<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
	xmlns:tmt="http://www.massimocorner.com/validator">
	<xsl:param name="DOTNET2"/>

	
	
	

	<xsl:template match="Instance">

		<xsl:variable name="Name" select="Name"/>
		

function Save<xsl:value-of select="Name"/> ( id )
{
	if ( id == null ) id = EMPTY_GUID;
	Loading();
	SetEnabled<xsl:value-of select="Name"/> ( false );
	<xsl:value-of select="Name"/>AJAX.GetLoaded ( null , null , Save<xsl:value-of select="Name"/>GetLoaded_CallBack , id);
}

function Save<xsl:value-of select="Name"/>GetLoaded_CallBack ( res )
{
	var obj = res.value;
	Set<xsl:value-of select="Name"/>ObjectFromForm ( obj ) ;
	obj.InstanceID = res.context;
	
	<xsl:value-of select="Name"/>AJAX.Save ( res.context != EMPTY_GUID , obj , Save<xsl:value-of select="Name"/>Saved_CallBack , obj.InstanceID );
}

function Save<xsl:value-of select="Name"/>Saved_CallBack ( res )
{
	if ( res.value )
	{
		alert ( 'Save Complete ' + res.context );
	}
	SetEnabled<xsl:value-of select="Name"/> ( true );
	Loaded();
}


function Load<xsl:value-of select="Name"/> ( id )
{
	if ( id == null ) id = EMPTY_GUID;
	Loading();
	SetEnabled<xsl:value-of select="Name"/> ( false );
	<xsl:value-of select="Name"/>AJAX.Load ( id != EMPTY_GUID , id , Load<xsl:value-of select="Name"/>_CallBack );

}

function Load<xsl:value-of select="Name"/>_CallBack ( res )
{
	var obj = res.value;
	
	Set<xsl:value-of select="Name"/>FormFromObject ( obj ) ;
	SetEnabled<xsl:value-of select="Name"/> ( true );
	Loaded();

}

	
function GetLoaded<xsl:value-of select="Name"/> ( name , value )
{
	Loading();
	SetEnabled<xsl:value-of select="Name"/> ( false );
	<xsl:value-of select="Name"/>AJAX.GetLoaded ( name , value , Load<xsl:value-of select="Name"/>GetLoaded_CallBack);
}

function Load<xsl:value-of select="Name"/>GetLoaded_CallBack ( res )
{
	var obj=res.value;
	Set<xsl:value-of select="Name"/>FormFromObject ( obj ) ;
	SetEnabled<xsl:value-of select="Name"/> ( true );
	Loaded();
}


function SetEnabled<xsl:value-of select="Name"/> ( val )
{
	<xsl:for-each select="ClassAttribs/ClassAttributes">if ( YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>') != null ) YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').disabled = !val;
	</xsl:for-each>
}




function Set<xsl:value-of select="Name"/>FormFromObject ( obj ) {

		<xsl:for-each select="ClassAttribs/ClassAttributes">

			<xsl:if test="CType='single'">
				<xsl:choose>
					<xsl:when test="SqlSPType='varchar(256)' or Type='int' or Type='double'">
	YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').value=obj.<xsl:value-of select="PropertyName"/>;
					</xsl:when>
					<xsl:when test="SqlSPType='ntext'">
	YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').value=obj.<xsl:value-of select="PropertyName"/>;
					</xsl:when>
					<xsl:when test="Type='Guid'">
	YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').innerHTML=obj.<xsl:value-of select="PropertyName"/>;
					</xsl:when>
					<xsl:when test="Type='DateTime'">
						YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>_DT').innerHTML=obj.<xsl:value-of select="PropertyName"/>_DT;
					</xsl:when>

					<xsl:when test="Type='bool'">
						YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').checked=obj.<xsl:value-of select="PropertyName"/>;

					</xsl:when>
				</xsl:choose>

			</xsl:if>

			<xsl:if test="IsEnum='true'">

				clearDropDownList('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>');

				populateDropDownList('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>', 
					obj.<xsl:value-of select="PropertyName"/>_VALS , obj.<xsl:value-of select="PropertyName"/>);

			</xsl:if>
			
			<xsl:if test="KnownClass='true'">
			
				clearDropDownList('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>');

				populateDropDownListClass('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>', 
					obj.<xsl:value-of select="PropertyName"/>_VALS , obj.<xsl:value-of select="PropertyName"/>.ObjectID);			
			
			</xsl:if>
			
			<!-- TODO : Add handling of complex types to this section so we can pick currency - also needs
				adding to the SetObjectFromForm - think it is already in the .cs file -->
		</xsl:for-each>
}


function HandleIncoming<xsl:value-of select="Name"/>() {
	Loading();
	<xsl:value-of select="Name"/>AJAX.GetInstance(HandleIncoming<xsl:value-of select="Name"/>GetInstance_CallBack);
}


function HandleIncoming<xsl:value-of select="Name"/>GetInstance_CallBack ( res )
{
	var obj = res.value;
	
	obj = Set<xsl:value-of select="Name"/>ObjectFromForm ( obj );
	
	<xsl:value-of select="Name"/>AJAX.HandleIncoming(obj, "", HandleIncoming<xsl:value-of select="Name"/>_CallBack);
}

function HandleIncoming<xsl:value-of select="Name"/>_CallBack(res) {
	Loaded();
	if ( !res.value )
	{
		alert ('Handling of <xsl:value-of select="Name"/> failed');
	} else
	{
	    
		alert ('To be Implemented');
	    
	}
}




function Get<xsl:value-of select="Name"/>Instance ( callBack , context ) {
		Loading();
		<xsl:value-of select="Name"/>AJAX.GetInstance ( callBack , context );
}



function Set<xsl:value-of select="Name"/>ObjectFromForm ( obj ) {
<xsl:for-each select="ClassAttribs/ClassAttributes">
		<xsl:if test="CType='single'">
			<xsl:choose>
				<xsl:when test="SqlSPType='varchar(256)' or Type='int' or Type='double'">
	obj.<xsl:value-of select="PropertyName"/>=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').value;
				</xsl:when>
				<xsl:when test="SqlSPType='ntext'">
	obj.<xsl:value-of select="PropertyName"/>=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').value;
				</xsl:when>
				<xsl:when test="Type='Guid'">
	obj.<xsl:value-of select="PropertyName"/>=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').innerHTML;
				</xsl:when>
				<xsl:when test="Type='DateTime'">
	obj.<xsl:value-of select="PropertyName"/>_DT=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>_DT').innerHTML;
				</xsl:when>
				<xsl:when test="Type='bool'">
	obj.<xsl:value-of select="PropertyName"/>=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').checked;
				</xsl:when>
			</xsl:choose>
		</xsl:if>
		<xsl:if test="IsEnum='true'">
	obj.<xsl:value-of select="PropertyName"/>=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').options[YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').selectedIndex].value;
		</xsl:if>
		
		<xsl:if test="KnownClass='true'">
	obj.<xsl:value-of select="PropertyName"/>Guid=YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').options[YAHOO.util.Dom.get('<xsl:value-of select="$Name"/>_<xsl:value-of select="Name"/>').selectedIndex].value;	
		</xsl:if>
	</xsl:for-each>	
		
	return obj;
}







	</xsl:template>
</xsl:stylesheet>